import QtQuick 2.0
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import "models"
import "rabbitmqmodel"

Item {
    id: id_root
    function build_good_request() {
        var data = {
            "measure_interval": 480
        }
        var req = {
            "condition": "description = 'current'",
            "name": "storage.put",
            "resource": "setup",
            "data": data
        }

        return JSON.stringify(req)
    }

    function build_bad_request() {
        var req = {
            "condition": "description = 'current'",
            "name": "storage.put",
            "resource": "setup",
            "data": "\"measure_interval\":480"
        }
        return JSON.stringify(req)
    }
    Text {
        id: id_page2_text
        text: qsTr("页面2")
        anchors.horizontalCenter: id_root.horizontalCenter
        anchors.top: id_root.top
    }
    RowLayout {
        id: id_rowlayout
        anchors.top: id_page2_text.bottom
        width: id_root.width
        height: 100
        Item {
            Layout.fillWidth: true
        }
        Button {
            text: "保存\n（返回正确）"
            onClicked: {
                id_edit.text = ""
                var req = build_good_request()
                RabbitmqManager.request(Utils._SERVICE_NAME_STORAGE,
                                        req).then(function (msg) {
                                            id_edit.text = msg
                                            id_rectangle.color = 'green'
                                            id_dialog.showMsg = "保存数据正确"
                                            id_dialog.visible = true
                                        }, function (msg) {
                                            id_edit.text = msg
                                            id_dialog.showMsg = "保存数据出错"
                                            id_dialog.visible = true
                                        })
            }
        }
        Button {
            text: "保存\n（返回错误）"
            onClicked: {
                id_edit.text = ""
                var req = build_bad_request()
                RabbitmqManager.request(Utils._SERVICE_NAME_STORAGE,
                                        req).then(function (msg) {
                                            id_edit.text = msg
                                        }, function (msg) {
                                            id_rectangle.color = 'red'
                                            id_edit.text = msg
                                            id_dialog.showMsg = "保存数据出错"
                                            id_dialog.visible = true
                                        })
            }
        }
        Item {
            Layout.fillWidth: true
        }
    }

    Flickable {
        id: id_flick
        anchors.top: id_rowlayout.bottom
        width: id_root.width
        height: 300
        contentWidth: id_edit.paintedWidth
        contentHeight: id_edit.paintedHeight
        clip: true
        function ensureVisible(r) {
            if (contentX >= r.x)
                contentX = r.x
            else if (contentX + width <= r.x + r.width)
                contentX = r.x + r.width - width
            if (contentY >= r.y)
                contentY = r.y
            else if (contentY + height <= r.y + r.height)
                contentY = r.y + r.height - height
        }

        TextEdit {
            id: id_edit
            width: id_flick.width
            focus: true
            wrapMode: TextEdit.Wrap
            onCursorRectangleChanged: id_flick.ensureVisible(cursorRectangle)
        }
    }

    Dialog {
        id: id_dialog
        visible: false
        title: "对话框"
        property string showMsg: ""
        anchors.centerIn: id_root
        contentItem: Rectangle {
            id: id_rectangle
            color: "lightskyblue"
            implicitWidth: 300
            implicitHeight: 100
            ColumnLayout {
                id: id_layout
                anchors.fill: id_rectangle
                Text {
                    id: id_dialog_text
                    text: id_dialog.showMsg
                    color: "navy"
                    Layout.alignment: Qt.AlignHCenter
                }
                Button {
                    text: "OK"
                    implicitWidth: 80
                    Layout.alignment: Qt.AlignHCenter
                    onClicked: {
                        id_dialog.visible = false
                    }
                }
            }
        }
    }
}
